package com.meihuak.cloud_ordering.controller;


import com.alibaba.fastjson.JSONObject;
import com.meihuak.cloud_ordering.bean.Student;
import com.meihuak.cloud_ordering.service.ManagerService;
import com.meihuak.cloud_ordering.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Slf4j
@Controller
@RequestMapping("/user")
public class StudentFormConntroller {

    @Autowired
    StudentService service;
    @Autowired
    ManagerService managerService;
    @ResponseBody
    @RequestMapping(value = "/add",method = POST)
    public Map<String, Object> getCode(@RequestParam(value="code",required=false)String code, HttpSession session){
        String openid = SendController.getOpenid(code);
        JSONObject convertvalue=new JSONObject();
        Student student = new Student();
        student.setOpenid(openid);
        if (service.getStudentByOpenid(openid)==null){

            service.save(student);

//
            convertvalue.put("msg","新用户创建成功");
        }else{
            convertvalue.put("msg","用户已存在");
        }
        Student stuinfo  = service.getStudentByOpenid(openid);
        session.setAttribute("stuid",stuinfo.getId());
        convertvalue.put("openid",openid);
        convertvalue.put("stuinfo",stuinfo);
        return convertvalue;
    }

    @ResponseBody
    @RequestMapping(value = "/del")
    public Map<String,String> delUser(){
        Map<String,String> map = new HashMap<>();
        map.put("msg","删除成功");//实际应用场景无需删除
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public Map<String,Object> update(@RequestBody Student student){
        Map<String,Object> map = new HashMap<>();
        try {
            service.saveOrUpdate(student);
            map.put("msg","修改成功");
        }catch (Exception e){
            map.put("msg","修改失败");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/info")
    public Map<String,Object> getInfo(@RequestParam(value = "id")String openid){
        Map<String,Object> map = new HashMap<>();
        try{
            Student student =  service.getById(openid);
            System.out.println(student.toString());
            map.put("msg","查询成功");
            map.put("info",student);
        }catch (Exception e){
            map.put("msg","查询失败");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/allstu" )
    public ModelAndView getAllInfo(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.isExist((int)session.getAttribute("id"))){
            List<Student> list = service.list();
            modelAndView.addObject("infos",list);
            modelAndView.setViewName("/manager/user");
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;

    }

    @ResponseBody
    @RequestMapping(value = "/allstuinfo" )
    public ModelAndView getAllUserInfo(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.isExist((int)session.getAttribute("id"))){
            List<Student> list = service.list();
            modelAndView.addObject("infos",list);
            modelAndView.setViewName("/manager/user1");
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;

    }

    @RequestMapping (value = "/allinfo")
    public String allInfo(Model m){
        return "student";
    }


    @ResponseBody
    @RequestMapping("/jsontest")
    public Map<String,Object> test(@RequestParam(value = "param")Map<String,Object> param){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","参数"+param.get("msg")+"已收到");
        return map;
    }


}
